package com.wyl.wyl.testapplication.utils;

import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

/**
 * @author 雷嘉
 * @time on 2019/4/3 11:35
 * @email jia.lei@inossem.com
 */
public class CollectionUtils {
    public static final String UNKNOWN = "unknown"; // 未知

    /**
     * 获取定制手机唯一标示：DeviceId(手机唯一标示，有可能是imei有可能是别的)+SerialNo(手机序列号)+CpuSerial(CPU序列号)
     *
     * @param context 上下文
     * @return 定制手机唯一标示
     * @author leij
     */
    public static String getDuid(Context context) {
        String result;
        String androidId = getAndroidDeviceId(context);
        String serialNo = getSerialNo();
//        String cpuSerial = getCPUSerial();
        String cpuSerial = getCpuName();
        result = androidId + ":" + serialNo + ":" + cpuSerial;
        return TextUtils.isEmpty(result) ? UNKNOWN : result;
    }

    public static String getAndroidDeviceId(Context context) {
        return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    /**
     * 获取手机序列号
     *
     * @return 手机序列号
     * @author leij
     */
    public static String getSerialNo() {
        String serialResult;
        serialResult = android.os.Build.SERIAL;
        return TextUtils.isEmpty(serialResult) ? UNKNOWN : serialResult;
    }
    public static String getCpuName() {

        String str1 = "/proc/cpuinfo";
        String str2 = "";

        try {
            FileReader fr = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(fr);
            while ((str2 = localBufferedReader.readLine()) != null) {
                if (str2.contains("Hardware")) {
                    return str2.split(":")[1];
                }
            }
            localBufferedReader.close();
        } catch (IOException e) {
        }
        return null;

    }
    /**
     * 获取CPU序列号
     *
     * @return CPU序列号
     * @author leij
     */
    public static String getCPUSerial() {
        String str, strCPU, cpuAddress = "0000000000000000";
        Process pp = null;
        InputStreamReader ir = null;
        LineNumberReader input = null;
        try {
            // 读取CPU信息
            pp = Runtime.getRuntime().exec("cat /proc/cpuinfo");
            ir = new InputStreamReader(pp.getInputStream());
            input = new LineNumberReader(ir);
            // 查找CPU序列号
            for (int i = 1; i < 100; i++) {
                str = input.readLine();
                if (str != null) {
                    // 查找到序列号所在行
                    if (str.contains("Serial")) {
                        // 提取序列号
                        strCPU = str.substring(str.indexOf(":") + 1, str.length());
                        // 去空格
                        cpuAddress = strCPU.trim();
                        break;
                    }
                } else {
                    // 文件结尾
                    break;
                }
            }
        } catch (Exception ex) {
            // 赋予默认值
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ir != null) {
                try {
                    ir.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (pp != null) {
                try {
                    pp.exitValue();
                } catch (IllegalThreadStateException e) {
                    pp.destroy();
                }
            }
        }
        return TextUtils.isEmpty(cpuAddress) ? UNKNOWN : cpuAddress;
    }
}